package com.smartstate.iot.tool;

import com.smartstate.iot.entity.TopicLog;
import com.smartstate.iot.service.TopicLogService;
import com.smartstate.iot.service.TopicService;
import com.smartstate.iot.vo.TopicUpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HandleService {
    @Autowired
    private TopicLogService topicLogService;

    @Autowired
    private TopicService topicService;


    /**
     * 添加设备信息
     *
     * @param topicLog
     * @return
     */
    public Boolean save(TopicLog topicLog) {
        boolean save = topicLogService.save(topicLog);
        System.out.println("save = " + save);
        return save;
    }

    /**
     * 判断当前信息设备信息是否正常
     *
     * @param content
     * @return
     */
    public Boolean getNull(TopicUpVo.Content content) {
        if (content.getMomery().equals("")) {
            return false;
        } else if (content.getIp().equals("")) {  // IP 地址校验  {\d+.\d+.\d+.\d+}
            return false;
        } else if (content.getCpu().equals("")) {   //
            return false;
        } else if (content.getPort().equals("")) {
            return false;
        } else if (content.getDeviceId().equals("")) {
            return false;
        } else if (content.getDeviceName().equals("")) {
            return false;
        } else if (content.getDeviceType().equals("")) {
            return false;
        }
        return true;
    }

    /**
     * 判断当前信息是新设备  还是已经存在设备
     * @param
     * @return
     */
        public Boolean logOnly(String deviceId) {
            Boolean integer = topicService.logOnly(deviceId);
            return integer;
    }


    /**
     * 判断日志是 新增   还是  修改
     *
     * @param content
     * @return
     */
    public Boolean getLog(TopicUpVo.Content content) {
        Boolean log = topicLogService.getLog(content);
        return log;
    }

//    /**
//     * 修改以往的日志信息
//     * @param topicLog
//     * @param content
//     * @return
//     */
//    public Boolean update(TopicLog topicLog, TopicUpVo.Content content) {
//        return topicLogService.updateByDeviceId(topicLog, content);
//    }

    /**
     * 查询同一台盒子上条日志状态是否相同
     *
     * @return
     */
    public Boolean getUpLogType(TopicLog topicLog) {
        TopicLog upLogType = topicLogService.getUpLogType(topicLog);
        System.out.println("upLogType = " + upLogType);
        String type = upLogType.getType();
        if (topicLog.getType().equals(type)) {
            // 相同
            return false;
        }
        //不相同
        return true;
    }
}
